#include "tui.h"
#include"memory.h"
#include<QMessageBox>
#include<QString>
#include<QImage>
#include<QPainter>
#include<QKeyEvent>
#include<QEvent>
#include<QRect>


boxGame::boxGame(){
    Map();
}
void boxGame::Map(){
    int graph[7][10]=
    {   {9,9,9,9,9,9,9,9,9,9},
        {9,0,1,0,0,0,1,7,0,9},
        {9,0,2,1,1,1,1,2,0,9},
        {9,0,0,0,0,0,0,0,0,9},
        {9,6,3,3,4,4,3,3,0,9},
        {9,0,3,0,5,0,0,3,8,9},
        {9,9,9,9,9,9,9,9,9,9}
    };//中间数组
    memcpy(&map[0][0],&graph[0][0],sizeof(int)*70);
}
void boxGame::drawmap(QPainter* Draw)
{QString imgURL;
    for(int i=0;i<7;i++){
        for(int j=0;j<10;j++){
            switch(map[i][j])
            {

case 0:imgURL=":/res/0_jpg.png";break;
case 1:imgURL=":/res/1_jpg.png";break;
case 2:imgURL=":/res/2_jpg.png";break;
case 3:case 4:imgURL=":/res/3_jpg.png";break;
case 5:imgURL=":/res/5_jpg.png";break;
case 6:imgURL=":/res/6_jpg.jpeg";break;
case 7:imgURL=":/res/7_jpg.png";break;
case 8:imgURL=":/res/8_jpg.jpg";break;
case 9:imgURL=":/res/9_jpg.jpg";break;
            }
            QImage img(imgURL);
            Draw->drawImage(QRect(j*img.width(),i*img.height(),img.width(),img.height()),img);

        }

    }
}
void boxGame::Keydown(QKeyEvent* event){

    int  i=getrow();
     int j =getcols();
    switch(event->key()){

    case Qt::Key_W:
    case Qt::Key_Up:

        if(map[i-1][j]==0)
        {
            map[i][j]-=7;
            map[i-1][j]=7;
        }
        if(map[i-1][j]==5)
        {map[i-1][j]=7;map[i][j]=0;
            QMessageBox msgBox;
            msgBox.setText("victory");
            msgBox.exec();
        }
        if(map[i-1][j]==6)
        {map[i][j]=0;
            map[i-1][j]=7;






        }
        if(map[i-1][j]==1){
            if(map[i-2][j]==0){
            map[i][j]-=7;
            map[i-1][j]=7;
            map[i-2][j]=1;
        }if(map[i-2][j]==8){
                map[i-2][j]=1;map[i-1][j]=7;map[i][j]=0;
                map[4][4]=0;
            }
        }
        break;
    case Qt::Key_S:
    case Qt::Key_Down:
        if(map[i+1][j]==0){
            map[i][j]-=7;
            map[i+1][j]=7;
        }if(map[i+1][j]==5){map[i+1][j]=7;map[i][j]=0;
            QMessageBox msgBox;
            msgBox.setText("victory");
            msgBox.exec();
        }if(map[i+1][j]==6){
            map[i+1][j]=7;map[i][j]=0;





        }

        if(map[i+1][j]==1){
            if(map[i+2][j]==0){
            map[i][j]-=7;
            map[i+1][j]=7;
            map[i+2][j]=1;
        }if(map[i+2][j]==8){
                map[i+2][j]=1;map[i+1][j]=7;map[i][j]=0;
                map[4][4]=0;
            }
        }
        break;
    case Qt::Key_A:
    case Qt::Key_Left:
        if(map[i][j-1]==0){
            map[i][j]=0;
            map[i][j-1]=7;
        }
        if(map[i][j-1]==5){
            map[i][j-1]=7;
            map[i][j]=0;
            QMessageBox msgBox;
            msgBox.setText("victory");
            msgBox.exec();
        }
        if(map[i][j-1]==6){
                    map[i][j-1]=7;
                    map[i][j]=0;




                }
        if(map[i][j-1]==1 ){
            if(map[i][j-2]==0){
            map[i][j]-=7;
            map[i][j-1]=7;
            map[i][j-2]=1;
        }if(map[i][j-2]==8){
                map[i][j-2]=1;map[i][j-1]=7;map[i][j]=0;
                map[4][4]=0;
        }
}
        break;
    case Qt::Key_D:
    case Qt::Key_Right:
        if(map[i][j+1]==0){
            map[i][j]-=7;
            map[i][j+1]=7;
        } if(map[i][j+1]==5){
            QMessageBox msgBox;
            msgBox.setText("victory");
            msgBox.exec();

        }if(map[i][j+1]==6){
            map[i][j+1]=7;
            map[i][j]=0;




        }

        if(map[i][j+1]==1){
            if(map[i][j+2]==0){
            map[i][j]-=7;
            map[i][j+1]=7;
            map[i][j+2]=1;
        } if(map[i][j+2]==8){
                map[i][j+2]=1;map[i][j+1]=7;map[i][j]=0;
                map[4][4]=0;
            }

        }

        break;
    }
}

int boxGame::getrow(){
    for(int i=0;i<5;i++){
        for(int j=0;j<10;j++){
            if(map[i][j]==7) return i;
        }
    }return -1;
}
int boxGame::getcols(){
    for(int i=0;i<5;i++){
        for(int j=0;j<10;j++){
            if(map[i][j]==7) return j;
        }
    }
    return -1;

}
bool boxGame::gameover(){
    for(int i=0;i<7;i++){
        for(int j=0;j<10;j++){
            if(map[i][j]==5){
                return false;
            }
        }
    }
return true;
}

